package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.UserAdressService;
import com.cskaoyan.service.UserAdressServiceimpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月27日 23:42
 */
@WebServlet("/admin/address/*")
public class AdminAddressServlet extends WdBaseServlet {
    UserAdressService adressService = new UserAdressServiceimpl();

    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        //是不是都有的
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        String order = request.getParameter("order");
        String sort = request.getParameter("sort");
        //请求的
        String name = request.getParameter("name");
        String userId = request.getParameter("userId");
        //
        CommonData data = adressService.query(page, limit, sort, order);
        //
        System.out.println("---------------------------------测试参数-----------------------------");
        System.out.print("page = " + page);
        System.out.print(" limit = " + limit);
        System.out.print(" order = " + order);
        System.out.print(" sort = " + sort);
        System.out.print(" name = " + name);
        System.out.print(" userId = " + userId);
        System.out.println();
        //
        //测试情况
        if (name == null && userId == null) {
            System.out.println("此时名字和用户号都是空的");
            return BaseRespVo.ok(data);
        }
        //当userId有值的时候（此时name为null或者""）
        if (userId != null && ("".equals(userId) == false) && (name == null || "".equals(name))) {
            System.out.println("进入根据用户id查询");
            Integer id = Integer.parseInt(userId);
            List<MarketAddress> marketAddresses = adressService.selectByuserId(id);
            CommonData commonData = CommonData.data(marketAddresses);
            return BaseRespVo.ok(commonData);
        }
        //当name有值的时候（此时userId为null或者""）
        if (name != null && ("".equals(name) == false) && (userId == null || "".equals(userId))) {
            System.out.println("进入根据用户名模糊查询");
            List<MarketAddress> list = adressService.selectByFuzzyName(name);
            CommonData commonData = CommonData.data(list);
            return BaseRespVo.ok(commonData);
        }
        return BaseRespVo.ok(data);
    }

}
